package flc.ast.adapter;

import android.graphics.Color;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import flc.ast.R;
import flc.ast.bean.MyBmiBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;

public class BmiAdapter extends StkProviderMultiAdapter<MyBmiBean> {

    public BmiAdapter() {
        addItemProvider(new StkSingleSpanProvider(30));
        addItemProvider(new BmiProvider());
    }

    private class BmiProvider extends BaseItemProvider<MyBmiBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_bmi;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyBmiBean bean) {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
            format.setTimeZone(TimeZone.getTimeZone("UTC"));
            baseViewHolder.setText(R.id.tvBmiItemDate, TimeUtils.millis2String(bean.getTime(), new SimpleDateFormat("yyyy/MM/dd")));
            baseViewHolder.setText(R.id.tvBmiItemTime, TimeUtils.millis2String(bean.getTime(), new SimpleDateFormat("HH:mm")));
            baseViewHolder.setText(R.id.tvBmiItemHeight, bean.getHeight() + "");
            baseViewHolder.setText(R.id.tvBmiItemWeight, bean.getWeight() + "");
            String formatted = String.format("%.1f", bean.getBmi());
            baseViewHolder.setText(R.id.tvBmiItemBmi, formatted);
            if (bean.getBmi() < 18.5) {
                baseViewHolder.setBackgroundColor(R.id.tvBmiItemBmi, Color.parseColor("#5B9CD6"));
            } else if (bean.getBmi() < 24) {
                baseViewHolder.setBackgroundColor(R.id.tvBmiItemBmi, Color.parseColor("#71AD46"));
            } else if (bean.getBmi() < 28) {
                baseViewHolder.setBackgroundColor(R.id.tvBmiItemBmi, Color.parseColor("#FFBF04"));
            }else {
                baseViewHolder.setBackgroundColor(R.id.tvBmiItemBmi, Color.parseColor("#BF0100"));
            }
        }
    }
}
